#include "FONT.H"

void drawFont(Fonts polygon, char ch,  int posX, int posY, byte color){
	if(polygon.map[ch]!=-1){
		drawPolygon(polygon.fonts[polygon.map[ch]], posX, posY, color);
	}
	else{
		drawPolygon(polygon.defaultSymbol, posX, posY, color);
	}	
}
void drawClippedScreenFont(Fonts polygon, char ch, int posX, int posY, byte color, Region clipPlane){
	if(polygon.map[ch]!=-1){
		drawClippedScreenPolygon(polygon.fonts[polygon.map[ch]], posX, posY, color, clipPlane);
	}
	else{
		drawClippedScreenPolygon(polygon.defaultSymbol, posX, posY, color, clipPlane);
	}
	
}
void drawClippedFont(Fonts polygon, char ch, int posX, int posY, byte color, Region clipPlane, Region viewPort){
	if(polygon.map[ch]!=-1){
		drawClippedPolygon(polygon.fonts[polygon.map[ch]], posX, posY, color, clipPlane, viewPort);
	}
	else{
		drawClippedPolygon(polygon.defaultSymbol, posX, posY, color, clipPlane, viewPort);
	}
	
}
boolean loadFonts(const char* fileName, Fonts* fonts){
	int fontWidth = 0;
	int fontHeight = 0;
	boolean retVal = TRUE;
	int charCount =0;
	int i =0;
	int j =0;
	int ch = 0;
	int verticesCount = 0;
	int x = 0;
	int y = 0;
	int pen = 0;
	int maxX = 0;
	int maxY = 0;
	FILE* fp = fopen(fileName, "r");
	if(fp != NULL){
		fscanf(fp, "%d", &charCount);
		fonts->charCount = charCount;
		fonts->fonts = (Polygon*)(malloc(sizeof(Polygon)*charCount));
		if(fonts->fonts != NULL){
			for(i=0;i<128;i++){
				fonts->map[i] = -1;
			}			
			for(i=0;i<charCount;i++){
				fscanf(fp, "%d %d", &ch, &verticesCount);				
				fonts->fonts[i].vertexCount = verticesCount;
				fonts->fonts[i].vertices = (Point*)(malloc(sizeof(Point)*verticesCount));
				if(fonts->fonts[i].vertices != NULL){
					maxX = 0;
					maxY = 0;					
					for(j=0;j<verticesCount;j++){
						fscanf(fp, "%d %d %d", &x, &y, &pen);				
						fonts->fonts[i].vertices[j].x = x;
						fonts->fonts[i].vertices[j].y = y;
						fonts->fonts[i].vertices[j].pen = pen;
						if(maxX < x){
							maxX = x;
						}
						if(maxY < y){
							maxY = y;
						}
					}	
					//printf("%d %d\n", maxX, maxY);
					fonts->fonts[i].width  = maxX;
					fonts->fonts[i].height  = maxY;
					fonts->map[ch] = i;
					if(fontWidth < maxX){
						fontWidth = maxX;
					}
					if(fontHeight < maxY){
						fontHeight = maxY;
					}
				}
				else{
					printf("Error allocating memory for font: %d\n", ch);
					retVal  = FALSE;
					break;
				}
			}
			fonts->defaultSymbol.vertexCount = 5;
			fonts->defaultSymbol.width = fontWidth;
			fonts->defaultSymbol.height = fontHeight;
			fonts->defaultSymbol.vertices = (Point*)(malloc(sizeof(Point)*5));
			fonts->defaultSymbol.vertices[0].x = 0;
			fonts->defaultSymbol.vertices[0].y = 0;
			fonts->defaultSymbol.vertices[0].pen = 1;
			fonts->defaultSymbol.vertices[1].x = fontWidth;
			fonts->defaultSymbol.vertices[1].y = 0;
			fonts->defaultSymbol.vertices[1].pen = 1;
			fonts->defaultSymbol.vertices[2].x = fontWidth;
			fonts->defaultSymbol.vertices[2].y = fontHeight;
			fonts->defaultSymbol.vertices[2].pen = 1;
			fonts->defaultSymbol.vertices[3].x = 0;
			fonts->defaultSymbol.vertices[3].y = fontHeight;
			fonts->defaultSymbol.vertices[3].pen = 1;
			fonts->defaultSymbol.vertices[4].x = 0;
			fonts->defaultSymbol.vertices[4].y = 0;
			fonts->defaultSymbol.vertices[4].pen = 1;
		}
		else{
			printf("Error allocating memory for fonts\n");
			retVal  = FALSE;
		}
		fclose(fp);
	}	
	else{
		printf("Error opening file\n");
		retVal  = FALSE;
	}
	return retVal;
}
void freeFonts(Fonts* fonts){
	int i;
	int count  = fonts->charCount;
	for(i=0;i<count;i++)
	{
		free(fonts->fonts[i].vertices);
	}
	free(fonts->fonts);
}
